package com.leyou.item.service.controller;

import com.leyou.common.bean.PageResult;
import com.leyou.common.bean.Result;
import com.leyou.item.inter.dto.SpecSelectDTO;
import com.leyou.item.inter.dto.SpuPageDTO;
import com.leyou.item.inter.dto.SpuSaveDTO;
import com.leyou.item.inter.dto.SpuUpdateDTO;
import com.leyou.item.inter.pojo.Sku;
import com.leyou.item.inter.pojo.SpecParam;
import com.leyou.item.inter.pojo.Spu;
import com.leyou.item.inter.pojo.SpuDetail;
import com.leyou.item.inter.vo.SkuLikeVO;
import com.leyou.item.inter.vo.SpuBoVO;
import com.leyou.item.service.service.GoodsService;
import com.leyou.item.service.service.SpecGroupParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zqq
 * @ClassName GoodsController
 * @Description: 商品相关的业务（包括SPU和SKU）
 * @date 2020/3/7-10:34
 */
@Api(value = "GoodsController", description = "商品相关的业务接口（包括SPU和SKU）")
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private SpecGroupParamService specGroupParamService;

    @PostMapping("spu/page")
    @ApiOperation(value = "分页查询商品列表", notes = "分页查询商品列表")
    public Result<PageResult<SpuBoVO>> querySpuBoByPage(@RequestBody SpuPageDTO dto) {
        return this.goodsService.querySpuBoByPage(dto);
    }

    @PostMapping("goods")
    @ApiOperation(value = "新增商品", notes = "新增商品")
    public Result saveGoods(@RequestBody SpuSaveDTO dto) {
        return this.goodsService.saveGoods(dto);
    }

    @GetMapping("spu/detail/{spuId}")
    @ApiOperation(value = "根据spuid查询spu详情", notes = "根据spuid查询spu详情")
    public Result<SpuDetail> querySpuDetailBySpuId(@ApiParam(name = "spuId", value = "spuId", required = true) @PathVariable("spuId") Long spuId) {
        return this.goodsService.querySpuDetailBySpuId(spuId);
    }

    @GetMapping("sku/list/{spuId}")
    @ApiOperation(value = "根据spuid查询有效的sku", notes = "根据spuid查询有效的sku")
    public Result<List<Sku>> querySkusBySpuId(@ApiParam(name = "spuId", value = "spuId", required = true) @PathVariable("spuId") Long spuId) {
        return this.goodsService.querySkusBySpuId(spuId,true);
    }

    @GetMapping("enable/sku/list/{spuId}")
    @ApiOperation(value = "根据spuid查询所有sku", notes = "根据spuid查询所有sku")
    public Result<List<Sku>> queryEnableSkusBySpuId(@ApiParam(name = "spuId", value = "spuId", required = true) @PathVariable("spuId") Long spuId) {
        return this.goodsService.querySkusBySpuId(spuId,false);
    }

    @PutMapping("goods")
    @ApiOperation(value = "修改商品", notes = "修改商品")
    public Result updateGoods(@RequestBody SpuSaveDTO dto){
        return this.goodsService.updateGoods(dto);
    }

    @GetMapping("spu/{id}")
    @ApiOperation(value = "根据spuid查询spu信息", notes = "根据spuid查询spu信息")
    public Result<Spu> querySpuById(@ApiParam(name = "id", value = "spuId", required = true) @PathVariable("id") Long id){
        return this.goodsService.querySpuById(id);
    }

    @GetMapping("sku/{id}")
    @ApiOperation(value = "根据skuid查询sku商品信息", notes = "根据skuid查询sku商品信息")
    public Result<Sku> querySkuById(@ApiParam(name = "id", value = "spuId", required = true) @PathVariable("id") Long id){
        return this.goodsService.querySkuById(id);
    }

    @GetMapping("sku/like/{num}")
    @ApiOperation(value = "猜你喜欢接口", notes = "猜你喜欢接口")
    public Result<List<SkuLikeVO>> querySkuByLike(@ApiParam(name = "num", value = "条数", required = true) @PathVariable("num") Integer num){
        return this.goodsService.querySkuByLike(num);
    }

    @PutMapping("update/spu")
    @ApiOperation(value = "spu上下架", notes = "spu上下架")
    public Result updateSpu(@RequestBody SpuUpdateDTO dto){
        return this.goodsService.updateGoods(dto);
    }

    @DeleteMapping("goods/{id}")
    @ApiOperation(value = "删除商品", notes = "删除商品")
    public Result deleteGoods(@ApiParam(name = "id",value = "商品id",required = true) @PathVariable("id") Long id){
        return this.goodsService.deleteGoods(id);
    }

    @PostMapping("goods/params")
    @ApiOperation(value = "根据规格参数条件查询规格参数", notes = "根据规格参数条件查询规格参数")
    public Result<List<SpecParam>> queryParams(@RequestBody SpecSelectDTO dto){
        return this.specGroupParamService.queryParams(dto);
    }
}
